package com.example.administrator.nurse.feature;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.model.CommonViewHolder;
import com.example.administrator.nurse.model.RecyclerViewCommonAdapter;
import com.example.administrator.nurse.model.bean.FeatureActivityBean;
import com.example.administrator.nurse.util.recyclerViewHelper;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/23.
 */
public class FeatureDataActivity extends BaseActivity {

    private List<FeatureActivityBean> list = new ArrayList<>();
    private final int[] colors = new int[]{R.drawable.circle_1,};
    private RecyclerViewCommonAdapter adapter;
    private recyclerViewHelper recyclerViewHelper;
    private LinearLayout container;
    private View selectTemp;
    private int selectIndex=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initAdapter(){
        for (int i=0;i<20;i++){
            FeatureActivityBean bean=new FeatureActivityBean();
            bean.setPatientInfo("王小二，女，68岁");
            bean.setDoctor("黄华佗");
            bean.setMeasureTime("2016-3-15 12:13:12");
            bean.setType("体温");
            bean.setType1("°C");
            list.add(bean);
        }
        adapter=new RecyclerViewCommonAdapter(this,list) {
            @Override
            protected int getLayoutId() {
                return R.layout.feature_data_item_layout;
            }

            @Override
            protected void bindView(CommonViewHolder holder, final int position) {
                LinearLayout firstFather;
                TextView patient,type,type1,doctor,measureTime,temperature;
                firstFather=holder.getLinearLayout(R.id.firstFather);
                patient=holder.getTextView(R.id.patient_info);
                type=holder.getTextView(R.id.type);
                type1=holder.getTextView(R.id.type1);
                doctor=holder.getTextView(R.id.doc);
                measureTime=holder.getTextView(R.id.time);
                //temperature=holder.getTextView(R.id.temperature);
                patient.setText(list.get(position).getPatientInfo());
                type.setText(list.get(position).getType());
                type1.setText(list.get(position).getType1());
                doctor.setText(list.get(position).getDoctor());
                measureTime.setText(list.get(position).getMeasureTime());
                //temperature.setText(list.get(position).getTempurature());
                firstFather.setBackgroundResource(colors[position % 2]);
                holder.getRelativeLayout(R.id.bean_father).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(null, position);
                    }
                });
            }
        };
        adapter.setOnItemClickListener(new RecyclerViewCommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    private void onItemSelectListener(final View... view){ /** 个数动态变化 */
        for(final View viewT : view){
            viewT.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selectTemp!=null){
                        /** 处理上一个被选中的 */
                        ((TextView)selectTemp.findViewById(R.id.text)).setTextColor(Color.GRAY);
                        selectTemp.findViewById(R.id.line).setVisibility(View.GONE);
                    }
                    selectTemp = viewT;
                    ((TextView)viewT.findViewById(R.id.text)).setTextColor(getResources().getColor(R.color.navbar)); /** 显示颜色 */
                    viewT.findViewById(R.id.line).setVisibility(View.VISIBLE); /** 显示出颜色 */
                    /** 下面标记 */
                    selectIndex = (Integer)viewT.getTag();
                }
            });
        }
    }

    private void initView(){
        configCommonHeader(FeatureDataActivity.this, getResources().getString(R.string.feature_data_title), 0);
        container= (LinearLayout) f(R.id.container);
        View room= LayoutInflater.from(this).inflate(R.layout.select_item,(ViewGroup)getWindow().getDecorView(),false);
        container.addView(room);
        selectTemp=room;
        View patient= LayoutInflater.from(this).inflate(R.layout.select_item, (ViewGroup) getWindow().getDecorView(), false);
        container.addView(patient);
        View recode= LayoutInflater.from(this).inflate(R.layout.select_item, (ViewGroup) getWindow().getDecorView(), false);
        container.addView(recode);
        initAdapter();
        onItemSelectListener(room,patient,recode);
        recyclerViewHelper=new recyclerViewHelper(this,(XRecyclerView)findViewById(R.id.recyclerView))
                .withAdapter(adapter)
                .withLayoutManager(null)
                .withPullRefrshOpen(false)
                .withLoadMore(new recyclerViewHelper.loadMoreListener() {
                    @Override
                    public void loadMore() {

                    }
                });
        recyclerViewHelper.getRecyclerView().loadMoreComplete();
    }

    @Override
    protected int setLayotId() {
        return R.layout.feature_data_layout;
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }
}
